﻿/**
 * Created in 2012-2-7.
 */
package kit4j.generator.keyless;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.List;

/**
 * 随机中文字体生成器。
 * 
 * @author MiniKnife
 *
 */
public class RandomChineseFontKeylessGenerator implements IKeylessGenerator<Font> {
	
	private static final Font[] ALL_FONTS = getAllFonts();
	
	private RandomIntegerKeylessGenerator numberGenerator;
	
	/**
	 * 创建一个随机中文字体生成器。
	 */
	public RandomChineseFontKeylessGenerator() {
		numberGenerator = new RandomIntegerKeylessGenerator(ALL_FONTS.length);
	}

	/**
	 * @see kit4j.generator.keyless.IKeylessGenerator#next()
	 */
	@Override
	public Font next() {
		return ALL_FONTS[numberGenerator.next()];
	}
	
	private static Font[] getAllFonts() {
		Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
		List<Font> fontList = new ArrayList<Font>();
		
		for(Font f : fonts) {
			if(f.canDisplayUpTo("剥傀俐晓玄座") == -1) {
				fontList.add(f);
			}
		}
		
		return fontList.toArray(new Font[0]);
	}

}
